// pages/info/detail/detail.js
const { apiUrl } = require("../../../app");
const {
  getObject,
  getCommentsByCommentID,
  getCommentsByObjectID,
  createComment,
} = require("../../../utils/apis");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    showObjPop: false,
    object: undefined,
    requestPage: {
      page: 1,
      size: 5,
    },
    reviewList: [],
  },
  /**
   * 评分组件函数
   * @param {*} event
   */
  onChange(event) {},

  /**
   * 防止事件冒泡给父组件
   */
  stopPropagation() {
    // 什么都不做
  },
  /**
   * 向后端发出请求根据id获取对象信息
   * @param {*} id
   */
  getObj: function (id) {
    getObject(id).then((res) => {
      const newObj = res.data.data;
      console.log("obj:", newObj);
      const currentTime = new Date();
      const updateTime = new Date(newObj.updated_at * 1000); // 假设 updateTime 是从服务器获取到的字段
      console.log(newObj.updated_at);
      const timeDifference =
        (currentTime.getTime() - updateTime.getTime()) / (1000 * 60);
      newObj.timeDifference = timeDifference;
      // 人物
      if (!newObj.type) {
        if (newObj.edges.teach_courses) {
          newObj.courseNumber = newObj.edges.teach_courses.length;
        } else {
          newObj.courseNumber = 0;
        }
      }
      // score
      if (newObj.edges.comments) {
        var sum = 0;
        var count = 0;
        for (var key in newObj.edges.comments) {
          sum += newObj.edges.comments[key].score;
          count++;
        }
        newObj.score = (sum / count).toFixed(1);
      }
      // comments
      if (newObj.edges.comments) {
        newObj.comments_num = newObj.edges.comments.length;
      } else {
        newObj.comments_num = 0;
      }
      this.setData({
        object: newObj,
      });
    });
  },

  /**
   * 从后端获取点评内容
   * @param {*} id
   */
  getReview: function (id) {
    getCommentsByObjectID(id).then((res) => {
      const newReview = res.data.data;
      this.setData({
        reviewList: newReview,
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const id = options.id;
    this.setData({
      id: id,
    });
    this.getObj(id);
    this.getReview(id);
  },
  onShow() {
    this.getObj(this.data.id);
    this.getReview(this.data.id);
  },
  /**
   * 获取评论
   * @param {*} reviewId
   */
  getComments(reviewId) {
    getCommentsByCommentID(reviewId).then((res) => {
      const comments = res.data.data;
      console.log(comments);
      this.setData({
        comments: comments,
      });
    });
  },
  /**
   * 弹出层展示
   */
  showPopup(event) {
    this.setData({
      writeComment: event.detail,
      showPop: true,
      showSearch: false,
    });
  },
  showObjPopup(event) {
    const obj = event.currentTarget.dataset.obj;
    this.setData({
      writeObj: obj,
      showObjPop: true,
      showSearch: false,
    });
  },
  /**
   * 弹出层关闭
   */
  onClose() {
    this.setData({ showPop: false });
    this.setData({
      comments: [],
      showSearch: true,
    });
  },
  onObjClose() {
    this.setData({
      showObjPop: false,
      showSearch: true,
    });
  },

  newComment(e) {
    const formData = e.detail.value;
    if (formData.type && formData.type === "99") {
      createComment({
        content: formData.content,
        score: 0,
        edges: {
          parent: {
            id: this.data.writeComment.id,
          },
        },
      })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    } else if (this.data.writeObj.type === 2) {
      createComment({
        content: formData.content,
        score: formData.score,
        edges: {
          object: {
            id: this.data.writeObj.id,
          },
          course_comment: {
            difficulty: formData.difficulty,
            quality: formData.quality,
            workload: formData.workload,
            mark: formData.mark,
          },
        },
      })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    } else {
      createComment({
        content: formData.content,
        score: formData.score,
        edges: {
          object: {
            id: this.data.writeObj.id,
          },
        },
      })
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          console.log(err);
        });
    }
    this.onObjClose();
    this.onClose();
    this.onShow();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      object: [],
    });
    this.getObj(this.data.id);
    this.getReview(this.data.id);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
